package com.twd.netty.xuleihua;

import com.alibaba.fastjson.JSON;
import io.netty.buffer.ByteBuf;

import java.nio.charset.StandardCharsets;

public class ResponseMessage {
   public Object messageBody;
    int clazz;

    public ResponseMessage(Object messageBody, int clazz) {
        this.messageBody = messageBody;
        this.clazz = clazz;
    }

    public ResponseMessage() {
    }

    public void encode(ByteBuf byteBuf){
        byteBuf.writeInt(clazz);
        byteBuf.writeBytes(JSON.toJSONString(messageBody).getBytes());
    }
    public void decode(ByteBuf byteBuf){
        clazz = byteBuf.readInt();
        messageBody = JSON.parseObject(byteBuf.toString(StandardCharsets.UTF_8), OperationType.values(clazz));
    }
}
